package com.scrapy.dao.mongdb.joker;

import java.util.List;

import com.scrapy.http.pojo.Reply;
import org.bson.Document;

import com.scrapy.dao.mongdb.util.DBUtil;
import com.scrapy.http.pojo.Joke;

/**
 * 对joke笑话封
 * 保存到数据库
 * @author wanter
 *
 */
public class JokeDao {
	private String connectionName = "joke";
	private DBUtil dbUtil = new DBUtil(connectionName);
	

	public void insert(List<Joke> lists){
		for (Joke joke : lists) {
			this.insert(joke);
		}
	}
	/**
	 * 单个插入
	 * @param joke
	 */
	public void insert(Joke joke){
		dbUtil.insert(this.getDocumentByList(joke));
	}
	
	
	/**
	 * 将一个笑话集转换为一个map对象的方法
	 * @param joke
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Document getDocumentByList(Joke joke){
		Document doc = new Document();
		//TODO
		doc.append("抓取时间", joke.getDate().toLocaleString());
		doc.append("抓取内容", joke.getContent());
		Document childoc = new Document();
		childoc.append("作者地址:", joke.getAuthorUrl());
		childoc.append("作者名字", joke.getAuthorName());
		childoc.append("评论条数", joke.getDash());
		childoc.append("喜欢数", joke.getStatsVote());
		List<Reply> lists = joke.getReplyList();
		Document replyDocument = new Document();
		for (int i = 0; i < lists.size(); i++) {
			Reply reply = lists.get(i);
			Document temp = new Document();
			temp.append("评论时间",reply.getDate());
			temp.append("评论的内容",reply.getMesg());
			temp.append("评论人的地址",reply.getReplyUserUrl());
			temp.append("评论楼层",reply.getFooter());
			replyDocument.append(reply.getFooter()+"楼评论",temp);
		}
		doc.append("评论",replyDocument);
		doc.append("author", childoc);
		return doc;
	}

}
